/* $Id: ProcessDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.ProcessDAO;
import ae.pcfc.etks.online.common.domain.AppMetaData;
import ae.pcfc.etks.online.common.domain.ProcStepActions;
import ae.pcfc.etks.online.common.domain.ProcStepsComnts;
import ae.pcfc.etks.online.common.domain.Process;
import ae.pcfc.etks.online.common.domain.ProcessStep;

@Repository
public class ProcessDAOImpl extends BaseDAO implements ProcessDAO{

	//TODO Move it to Process Step
	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ProcessDAO#add(ae.pcfc.etks.online.common.domain.ProcessStep)
	 * 
	 */
	public boolean add(ProcessStep processStep) {
		
		
		getHibernateTemplate().getSessionFactory().getCurrentSession().save(processStep);		
		
		Iterator<ProcStepsComnts> itr = processStep.getProcStepsComntses().iterator();
		
		while(itr.hasNext())
		{			
			ProcStepsComnts stepsComnts = itr.next();	
			getHibernateTemplate().getSessionFactory().getCurrentSession().save(stepsComnts);
		}	
		
		Iterator<ProcStepActions> itrr = processStep.getProcStepActionses().iterator();
		
		while(itrr.hasNext())
		{
			ProcStepActions stepActions = itrr.next();			
			getHibernateTemplate().getSessionFactory().getCurrentSession().save(stepActions);
		}	
		
		return true;
	}
	
	
	//TODO Move it to AppMetaData DAO
	/**
	 * getAppMetaDataByApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ProcessDAO#getAppMetaDataByApplicationId(java.lang.Long)
	 * 
	 */
	public AppMetaData getAppMetaDataByApplicationId(Long applicationId){
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(AppMetaData.class);
		criteria.add(Restrictions.eq("applicationId", applicationId));
		
		@SuppressWarnings("unchecked")		
		List<AppMetaData> list =  criteria.list();
		
		if(list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * getProcessByProcessAndStepCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ProcessDAO#getProcessByProcessAndStepCode(java.lang.String, java.lang.String)
	 * 
	 */
	public ae.pcfc.etks.online.common.domain.Process getProcessByProcessAndStepCode(String processCode, String processStepCode) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ae.pcfc.etks.online.common.domain.Process.class, "process");
		criteria.add(Restrictions.eq("process.processCode", processCode));
		criteria.createAlias("process.processSteps", "processStep");		
		criteria.add(Restrictions.eq("processStep.processStepCode", processStepCode));
		@SuppressWarnings("unchecked")		
		List<ae.pcfc.etks.online.common.domain.Process> list =  criteria.list();
		
		if(list.size() > 0){
			System.out.println("Process list-->"+list.get(0));
			return list.get(0);
			
		}
		else
			return null;
	}
	
	/**
	 * getProcessByProcessCode
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ProcessDAO#getProcessByProcessCode(java.lang.String)
	 * 
	 */
	public ae.pcfc.etks.online.common.domain.Process getProcessByProcessCode(String processCode) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ae.pcfc.etks.online.common.domain.Process.class, "process");
		criteria.add(Restrictions.eq("process.processCode", processCode));
		criteria.add(Restrictions.eq("process.processStatus", ConstantsCommon.ACTIVE).ignoreCase());
		@SuppressWarnings("unchecked")		
		List<ae.pcfc.etks.online.common.domain.Process> list =  criteria.list();
		
		if(list.size() > 0){
			return list.get(0);
		}
		else
			return null;
	}


	public List<Process> getAllProcesses() {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ae.pcfc.etks.online.common.domain.Process.class, "process");
		List<Process> list =  criteria.list();
		
		if(list.size() > 0){
			return list;
		}
		else
			return null;
	}

}
